﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TD.Interface.Credits.Effects
{
    internal enum RotateAxis : int
    {
        X = 0x00,
        Y = 0x01,
        Z = 0x02
    }

    internal class Efx_Rotate : Effect
    {
        private float angle;
        private Matrix matrix;
        private RotateAxis rotateAxis;

        public Efx_Rotate() :
            this(RotateAxis.Y, 0)
        {
            // Do something...
        }

        public Efx_Rotate(RotateAxis rotateAxis, int milisecondsDelay) :
            base(milisecondsDelay)
        {
            ResetAngle();
            this.matrix = Matrix.Identity;
            this.rotateAxis = rotateAxis;
        }

        protected override void TriggerEfx()
        {
            angle += 1.0f;
            if (angle >= 360.0f)
                ResetAngle();

            float radians = ((float)(Math.PI * angle / 180.0f));

            if (rotateAxis == RotateAxis.X)
            {
                matrix = Matrix.CreateRotationX(radians);
            }
            else if (rotateAxis == RotateAxis.Y)
            {
                matrix = Matrix.CreateRotationY(radians);
            }
            else
            {
                matrix = Matrix.CreateRotationZ(radians);
            }

            base.TriggerEfx();
        }

        private void ResetAngle()
        {
            angle = 0.0f;
        }

        protected override Matrix[] ApplyMatrices()
        {
            return new Matrix[1] { matrix };
        }
    }
}
